package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Property;
import com.ruoyi.system.service.IPropertyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-01-11
 */
@Controller
@RequestMapping("/system/property")
public class PropertyController extends BaseController
{
    private String prefix = "system/property";

    @Autowired
    private IPropertyService propertyService;

    @RequestMapping("/selectPropertyById")
    public String selectPropertyById(long id, ModelMap mmap){
        Property property = propertyService.selectPropertyById(id);
        Date date = property.getPurchaseDate();
        DateFormat dateInstance = DateFormat.getDateInstance();
        System.out.println(dateInstance.format(date));
        mmap.put("purchaseDate",dateInstance.format(date));
        mmap.put("property",property);
        return prefix + "/detail";
    }

    @RequiresPermissions("system:property:view")
    @GetMapping()
    public String property()
    {
        return prefix + "/property";
    }


    /**
     * 打印【请填写功能名称】
     */
    @RequiresPermissions("system:property:edit")
    @GetMapping("/Print/{id}")
    public String Print(@PathVariable("id") Long id, ModelMap mmap)
    {
        Property property = propertyService.selectPropertyById(id);
        mmap.put("property", property);
        return prefix + "/print";
    }


    @RequiresPermissions("system:property:toPrint")
    @GetMapping("/toPrint")
    public String toPrint(long id, ModelMap mmap)
    {
        // System.out.println(id);
        Property property = propertyService.selectPropertyById(id);
        System.out.println(property);
        String url = "http://192.168.50.31:8080/system/property/selectPropertyById?id="+id;
        mmap.put("url",url);
        mmap.put("propertyNumber",property.getPropertyNumber());
        mmap.put("propertyName",property.getPropertyName());
        mmap.put("configurationInfo",property.getConfigurationInfo());
        return prefix + "/print";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:property:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Property property)
    {
        startPage();
        List<Property> list = propertyService.selectPropertyList(property);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
    @RequiresPermissions("system:property:list")
    @GetMapping("/selectPropertyById")
    @ResponseBody
    public Property list(Long id)
    {
        Property property = propertyService.selectPropertyById(id);
        return property;
    }*/

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:property:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Property property)
    {
        List<Property> list = propertyService.selectPropertyList(property);
        ExcelUtil<Property> util = new ExcelUtil<Property>(Property.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 导入【请填写功能名称】列表
     */
    @Log(title = "资产管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:property:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Property> util = new ExcelUtil<>(Property.class);
        List<Property> propertyList = util.importExcel(file.getInputStream());
        String message = propertyService.importProperty(propertyList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 模板下载【请填写功能名称】列表
     */
    @RequiresPermissions("system:property:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Property> util = new ExcelUtil<>(Property.class);
        return util.importTemplateExcel("资产数据");
    }


    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:property:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Property property)
    {
        return toAjax(propertyService.insertProperty(property));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:property:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Property property = propertyService.selectPropertyById(id);
        mmap.put("property", property);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:property:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Property property)
    {
        return toAjax(propertyService.updateProperty(property));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:property:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(propertyService.deletePropertyByIds(ids));
    }
}
